package com.twu.biblioteca.session;

import com.twu.biblioteca.core.User;

import java.util.List;

/**
 * Created by cristianpinto on 2/21/14.
 */
public class UserSession {

    List<User> users;
    private User currentUser;

    public UserSession(List<User> users) {
        this.users = users;
        currentUser = null;
    }

    public boolean logIn(String libraryNumber, String password) {
        User theUser = findUser(libraryNumber);
        boolean userLoggedIn = theUser.isValidPassword(password);
        currentUser = userLoggedIn ? theUser : null;
        return userLoggedIn;
    }

    public User findUser(String libraryNumber) {
        for (User user : users) {
            if(user.getLibraryNumber().equals(libraryNumber))
                return user;
        }
        return null;
    }

    public User getCurrentUser() {
        return currentUser;
    }
}
